//
//  LXViewController.m
//  微博
//
//  Created by imera on 15-9-10.
//  Copyright (c) 2015年 imera. All rights reserved.
//

#import "LXViewController.h"
#import "LXHomeViewController.h"
#import "LXMessageViewController.h"
#import "LXDiscoverViewController.h"
#import "LXMineViewController.h"
#import "UIImage+Lx.h"
#import "LXTabBarView.h"
#import "LXNavigationViewController.h"
#import "LXComposeViewController.h"

@interface LXViewController ()<LXTabBarViewDelegate>

@property (nonatomic, weak) LXTabBarView *customTabBar;

@end

@implementation LXViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    //自定义TabBar
    [self setupTabBar];
    //添加自控制器
    [self setupAllChildControllers];

}

//删除以前的TabBarButton
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    for(UIView *child in self.tabBar.subviews)
    {
        if([child isKindOfClass:[UIControl class]])
        {
            [child removeFromSuperview];
        }
    }
}

//自定义tabbar
- (void)setupTabBar
{
    LXTabBarView *customTaBar = [[LXTabBarView alloc]init];
    customTaBar.frame = self.tabBar.bounds;
    [self.tabBar addSubview:customTaBar];
    
    customTaBar.MyDelegate = self;
    self.customTabBar = customTaBar;

}

//设置所有的子控制器
- (void)setupAllChildControllers
{
    //1.首页
    LXHomeViewController *home = [[LXHomeViewController alloc]init];
    home.tabBarItem.badgeValue = @"new";
    [self setupChildViewController:home title:@"首页" imageName:@"tabbar_home" selectedImage:@"tabbar_home_selected"];
    
    //2.消息
    LXMessageViewController *message = [[LXMessageViewController alloc]init];
    //message.tabBarItem.badgeValue = @"3";
    [self setupChildViewController:message title:@"消息" imageName:@"tabbar_message_center" selectedImage:@"tabbar_message_center_selected"];
    
    //3.广场
    LXDiscoverViewController *discover = [[LXDiscoverViewController alloc]init];
    //discover.tabBarItem.badgeValue = @"12";
    [self setupChildViewController:discover title:@"广场" imageName:@"tabbar_discover" selectedImage:@"tabbar_discover_selected"];
    
    //4.我
    LXMineViewController *mine = [[LXMineViewController alloc]init];
    //mine.tabBarItem.badgeValue = @"9";
    [self setupChildViewController:mine title:@"我的" imageName:@"tabbar_profile" selectedImage:@"tabbar_profile_selected"];
    

}

//添加子控制器
- (void)setupChildViewController:(UIViewController *)vc title:(NSString *)title imageName:(NSString *)imageName selectedImage:(NSString *)selectedImage
{
    vc.title = title;
    vc.tabBarItem.image = [UIImage imageWithName:imageName];
    vc.tabBarItem.selectedImage = [UIImage imageWithName:selectedImage];

    LXNavigationViewController *nav = [[LXNavigationViewController alloc]initWithRootViewController:vc];
    
    [self addChildViewController:nav];
    [self.customTabBar addTabBarButton:vc.tabBarItem];
}

//代理方法
- (void)tabBar:(LXTabBarView *)tabBar selectedButtonFrom:(int)from to:(int)to
{
    self.selectedIndex = to;
}

- (void)tabBarDidClicked:(LXTabBarView *)tabBar
{
    LXComposeViewController *composeVc = [[LXComposeViewController alloc]init];
    LXNavigationViewController *nav = [[LXNavigationViewController alloc]initWithRootViewController:composeVc];
    [self presentViewController:nav animated:YES completion:nil];
}
@end
